package com.yu.controller;

import com.yu.bean.User;
import com.yu.bean.Users;
import com.yu.service.UserInfoGetService;
import com.yu.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yu.websocket.common.WebSocketServer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class MainController {
    @Autowired
    UserInfoGetService UIS;

    @Resource
    private WebSocketServer webSocketServer;

    private void setUser(Model model)
    {
        String currentName = SecurityUtil.getCurrentUserName();
        if(!currentName.equals("null")){
            User Us = UIS.getUser(currentName);
            model.addAttribute("current_user_name",currentName);
            model.addAttribute("current_user_MHP",Us.getMHP());
            model.addAttribute("current_user_HP",Us.getHP());
            model.addAttribute("current_user_AC",Us.getAC());
            model.addAttribute("current_user_MMP",Us.getMMP());
            model.addAttribute("current_user_MP",Us.getMP());
            model.addAttribute("current_user_Item_list",Us.getItem_list());
            model.addAttribute("current_user_Task",Us.getTask());
        }
    }

    private void setCurrentName(Model model){
        String currentName = SecurityUtil.getCurrentUserName();
        //System.err.println(currentName);
        if(!currentName.equals("null")){
            model.addAttribute("current_user_name",currentName);
        }
    }

    @RequestMapping("/")
    public String root(){
        return "redirect:/welcome";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "welcome";
    }

    @RequestMapping("/common/info")
    public String commonUserInfo(Model model){
        setUser(model);
        return "common/info";
    }

    @RequestMapping("/admin/info")
    public String adminInfo(Model model){
        setUser(model);
        return "admin/info";
    }

    @RequestMapping("/admin/see")
    public String adminSee(Model model){
        List<User> Us = UIS.getAllUser();
        model.addAttribute("users",Us);
        return "admin/see";
    }


    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/login_error")
    public String loginError(Model model){
        model.addAttribute("loginError", true);
        return "login";
    }

    @RequestMapping("/use_card")
    public String use_card(Model model,String username){
        String currentName = SecurityUtil.getCurrentUserName();
        User cu = UIS.getUser(currentName);
        User u = UIS.getUser(username);
        u.setHP(u.getHP()-cu.getAC());
        UIS.updateuser(u);
        setUser(model);
        webSocketServer.sendInfo(username,"update");
        return "redirect:/common/info";
    }
    @RequestMapping("/admin_init")
    public String admin_init(Model model){
        List<User> Us = UIS.getAllUser();
        for(User u:Us)
        {
            u.setMP(0);
            u.setItem_list("");
            u.setHP(u.getMHP());
            u.setTask("");
            UIS.updateuser(u);
        }
        setUser(model);
        try {
            webSocketServer.onMessage("update");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/info";
    }

    @RequestMapping("/admin_see_set")
    public String admin_see_set(Model model, Users us){
        for(User u:us.getUsers())
        {
            UIS.updateuser(u);
        }
        setUser(model);
        try {
            webSocketServer.onMessage("update");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/see";
    }
    @RequestMapping("/user_set")
    public String use_set(Model model,String username,String password){
        User u = UIS.getUser(username);
        u.setPassword(password);
        UIS.updateuser(u);
        setUser(model);
        return "redirect:/admin/info";
    }
    @RequestMapping("/user_set_HP")
    public String use_set_HP(Model model,String username,String HP){
        User u = UIS.getUser(username);
        u.setHP(Integer.valueOf(HP));
        UIS.updateuser(u);
        setUser(model);
        try {
            webSocketServer.onMessage("update");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/info";
    }
    @RequestMapping("/user_set_MP")
    public String use_set_MP(Model model,String username,String MP){
        User u = UIS.getUser(username);
        u.setMP(Integer.valueOf(MP));
        UIS.updateuser(u);
        setUser(model);
        try {
            webSocketServer.onMessage("update");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/info";
    }
    @RequestMapping("/user_set_Item_list")
    public String use_set_text(Model model,String username,String Item_list){
        User u = UIS.getUser(username);
        u.setItem_list(Item_list);
        UIS.updateuser(u);
        setUser(model);
        setUser(model);
        try {
            webSocketServer.onMessage("update");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/info";
    }

    @RequestMapping("/chat")
    public String chat(Model model){
        setUser(model);
        return "chat";
    }
//    弹窗测试见面
//    @RequestMapping("/admin/alert")
//    public String admin_alert(Model model){
//        setUser(model);
//        return "/admin/alert";
//    }
}
